package com.bigboy.bbs.service;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.bigboy.bbs.util.RenameFile;
import com.oreilly.servlet.MultipartRequest;

public class CosUtil {
	private MultipartRequest multipartRequest;
	public CosUtil(HttpServletRequest request){
		String savePath = request.getSession().getServletContext().getRealPath("/")+"upload"+File.separator;
		int max = 1024 * 1024 * 10;
		try {
			multipartRequest = new MultipartRequest(request, savePath, max, "UTF-8", new RenameFile());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public String getParameter(String key){
		return multipartRequest.getParameter(key);
	}
	@SuppressWarnings("unchecked")
	public Map<String,String> getPicNames(){
		Enumeration filenames = multipartRequest.getFileNames();
		Map<String ,String> map = new HashMap<String,String>();
		String pic = "1.gif";
		while(filenames.hasMoreElements()){
			String filename = (String) filenames.nextElement();
			if(filename != null){
				pic = multipartRequest.getFilesystemName(filename);
				map.put(filename,pic );
			}
		}
		return map;
	}
}
